package com.zzyl.nursing.controller;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.service.IElderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 老人Controller
 *
 * @author ruoyi
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/elder" )
@Tag(name = "老人相关接口" )
public class ElderController extends BaseController {
    @Autowired
    private IElderService elderService;

    /**
     * 查询老人列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:elder:list')" )
    @GetMapping("/list" )
    @Operation(summary = "查询老人列表" )
    public TableDataInfo list(Elder elder) {
        startPage();
        List<Elder> list = elderService.selectElderList(elder);
        return getDataTable(list);
    }

    /**
     * 导出老人列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:elder:export')" )
    @Log(title = "老人" , businessType = BusinessType.EXPORT)
    @Operation(summary = "导出老人列表" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, Elder elder) {
        List<Elder> list = elderService.selectElderList(elder);
        ExcelUtil<Elder> util = new ExcelUtil<Elder>(Elder.class);
        util.exportExcel(response, list, "老人数据" );
    }

    /**
     * 获取老人详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:elder:query')" )
    @GetMapping("/{id}" )
    @Operation(summary = "获取老人详细信息" )
    public R<Elder> getInfo(@Schema(title = "老人ID" )
                            @PathVariable("id" ) Long id) {
        return R.ok(elderService.selectElderById(id));
    }

    /**
     * 新增老人
     */
    @PreAuthorize("@ss.hasPermi('nursing:elder:add')" )
    @Log(title = "老人" , businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增老人" )
    public AjaxResult add(@Schema(title = "老人实体" ) @RequestBody Elder elder) {
        return toAjax(elderService.insertElder(elder));
    }

    /**
     * 修改老人
     */
    @PreAuthorize("@ss.hasPermi('nursing:elder:edit')" )
    @Log(title = "老人" , businessType = BusinessType.UPDATE)
    @PutMapping
    @Operation(summary = "修改老人" )
    public AjaxResult edit(@Schema(title = "老人实体" ) @RequestBody Elder elder) {
        return toAjax(elderService.updateElder(elder));
    }

    /**
     * 删除老人
     */
    @PreAuthorize("@ss.hasPermi('nursing:elder:remove')" )
    @Log(title = "老人" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    @Operation(summary = "删除老人" )
    public AjaxResult remove(@Schema(title = "老人ID数组" ) @PathVariable Long[] ids) {
        return toAjax(elderService.deleteElderByIds(ids));
    }
}
